#include "connectionselector.h"
#include "ui_connectionselector.h"

ConnectionSelector::ConnectionSelector(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConnectionSelector)
{
    ui->setupUi(this);
    //connect(ui->connectionsList, SIGNAL(highlighted(QUrl)), this, SLOT(modifyConnection(QUrl)));
    connect(ui->connectionsList, SIGNAL(anchorClicked(QUrl)), this, SLOT(modifyConnection(QUrl)));
    connect(ui->EditConnectionPB, SIGNAL(clicked()),this,SLOT(editConnection()));
    connect(ui->AddConnectionPB, SIGNAL(clicked()),this,SLOT(addConnection()));
    connect(ui->RemoveConnectionPB, SIGNAL(clicked()), this,SLOT(removeConnection()));
}

ConnectionSelector::~ConnectionSelector()
{
    delete ui;
}

void ConnectionSelector::setConnectionInteface(ConnectionInterface* CI)
{
    _CI = CI;

    //FOR TESTING
//    _CI->newConnection("Test1","blackhawk.eng.uah.edu", 20152, "UAH03-1","23955","r/-86/34/150",true);
//    _CI->newConnection("Test2","blackhawk.eng.uah.edu", 20154, "UAH03-2","23955","",true);

//    _CI->connectToServer(0);
//    _CI->connectToServer(1);
    populateList();
}

void ConnectionSelector::populateList()
{
    _ServerList = _CI->getConnectionNames();
    for(int i = 0; i < _ServerList.size(); i++)
    {
        qDebug() << _ServerList.at(i);
        ui->connectionsList->append("<a href=" + _ServerList.at(i) + ">" + _ServerList.at(i) + "</a>");
    }
}

void ConnectionSelector::modifyConnection(QUrl connection)
{
    qDebug() << "ModifyCOnnection entered";
    QString cName = connection.toString();
    int found = _ServerList.indexOf(cName, 0);

    if(found > -1)
    {
        ui->ConnectionNameLE->setText(_CI->getName(found));
        ui->ServerLE->setText(_CI->getHostName(found));
        ui->portLE->setText(QString::number(_CI->getPortNumber(found)));
        ui->PasswordLE->setText(_CI->getPassword(found));
        ui->UsernameLE->setText(_CI->getUserName(found));
        ui->FilterLE->setText(_CI->getFilterString(found));
    }
}

void ConnectionSelector::addConnection()
{
    if(_ServerList.contains(ui->ConnectionNameLE->text()))
    {
        QApplication::beep();
    }
    else
    {
        _CI->newConnection(ui->ConnectionNameLE->text(), ui->ServerLE->text(), ui->portLE->text().toInt(), ui->UsernameLE->text(), ui->PasswordLE->text(), ui->FilterLE->text(), ui->TxAllowCB->isChecked());
        ui->connectionsList->clear();
        populateList();
        if(ui->ConnectedCB->isChecked())
            _CI->connectToServer(_ServerList.size()-1);

    }
}
void ConnectionSelector::removeConnection()
{
    int index = _ServerList.indexOf(ui->ConnectionNameLE->text(),0);
    if(!_ServerList.contains(ui->ConnectionNameLE->text()))
    {
        QApplication::beep();
    }
    else
    {
        _CI->removeConnection(index);
        ui->connectionsList->clear();
        populateList();
    }
}

void ConnectionSelector::editConnection()
{
    int index = _ServerList.indexOf(ui->ConnectionNameLE->text(), 0);
    if(!ui->ConnectedCB->isChecked())
    {
        _CI->disconnectFromServer(index);
        ui->ConnectedCB->setChecked(false);
    }
    else
    {
        _CI->disconnectFromServer(index);
        _CI->changeParameters(ui->ConnectionNameLE->text(), ui->ServerLE->text(), ui->portLE->text().toInt(), ui->UsernameLE->text(), ui->PasswordLE->text(), ui->FilterLE->text(), ui->TxAllowCB->isChecked());
        _CI->connectToServer(index);
    }
    ui->connectionsList->clear();
    populateList();
}
